<template>
  <div class="main">
    <div class="search">
      <div>
        <h2>考勤情况</h2>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
    </div>
    <div>
      <Search @searchclick="searchclick" />
    </div>
    <TableList
      :loading="loading"
      :tabledata="tableData"
      :params="params"
      :tabledatacolumn="tableDataColumn"
      :isshow="true"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
    >
      <template v-slot:default="info">
<!--        <el-button type="text" @click="chakan(info.info)">查看</el-button>-->
        <el-button v-if="getToken() === 'admin'" type="text" @click="zhiding(info.info)">{{ info.info.topContent===0?"置顶":"取消置顶" }}</el-button>
        <el-button v-if="getToken() === 'admin'" type="text" @click="tuijian(info.info)">{{ info.info.niceContent===0?"推荐":"取消推荐" }}</el-button>
        <el-button v-if="getToken() === 'admin'" type="text" @click="editcontent(info.info)">编辑</el-button>
        <el-button type="text" @click="deleteRow(info.info)">删除</el-button>
      </template>
    </TableList>
    <el-dialog title="查看" :visible.sync="dialogck" width="900px">
      <div>
        <h4>{{ details.title }}</h4>
        <div v-html="details.content" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { columnAdd, columnqueryAll, columndelete, contentApiqueryPage, contentApidelete, nice, top } from '@/api/webpage'

import TableList from '@/components/TableList.vue'
import Search from '@/components/search.vue'
export default {
  name: 'Tzgglist',
  components: {
    TableList, Search
  },
  data() {
    return {
      dialogck: false,
      details: {},
      loading: true,
      options: [],
      tableData: [],
      tableDataColumn: [
        { label: '标题', key: 'title' },
        { label: '审核人', key: 'postedBy' },
        { label: '是否置顶', key: 'isTop' },
        { label: '是否推荐', key: 'isNice' }
      ],
      params: {
        approval: 1,
        total: 0,
        pageNum: 1,
        pageSize: 10,
        columnId: 'FE9D74C4870047468AC6F8267C5E1481'
      }
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    chakan(row) {
      this.dialogck = true
      this.details = row
    },
    searchclick(val) {
      this.loading = true
      delete val.date
      for (const i in val) {
        this.params[i] = val[i]
      }
      this.getlist()
    },
    zhiding(info) {
      if (info.topContent === '1') {
        this.settop('0', info.id, '取消置顶')
      } else {
        this.settop('1', info.id, '置顶')
      }
    },
    settop(num, id, word) {
      top({ id: id, type: num }).then(res => {
        if (res.data === 1) {
          this.$message.success(word + '成功！')
          this.getlist()
        } else {
          this.$message.error(word + '成功！')
        }
      })
    },
    tuijian(info) {
      if (info.niceContent === '1') {
        this.setnice('0', info.id, '取消推荐')
      } else {
        this.setnice('1', info.id, '推荐')
      }
    },
    setnice(num, id, word) {
      nice({ id: id, type: num }).then(res => {
        if (res.data === 1) {
          this.$message.success(word + '成功！')
          this.getlist()
        } else {
          this.$message.error(word + '成功！')
        }
      })
    },
    add() {
      this.$router.push({
        name: 'addform',
        query: {
          cid: this.params.columnId
        }
      })
    },
    editcontent(row) {
      this.$router.push({
        name: 'addform',
        query: { type: 'edit', id: row.id, cid: this.params.columnId }
      })
    },
    getlist() {
      contentApiqueryPage(this.params).then((res) => {
        if (res.code === 0) {
          this.loading = false
          this.tableData = res.rows.map(item => {
            item.isTop = item.topContent === '1' ? '是' : '否'
            item.isNice = item.niceContent === '1' ? '是' : '否'
            return item
          })
          this.params.total = res.total
        }
      })
    },
    handleCurrentChange(val) {
      this.params.pageNum = val
      this.getlist()
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getlist()
    },
    deleteRow(row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const ids = []
          ids.push(row.id)
          contentApidelete(ids)
            .then((res) => {
              if (res.data === 1) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getlist()
              } else {
                this.$message.error('删除失败')
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => { })
    }
  }
}
</script>
<style scoped>
@import "~@/assets/css/adminstyle.css";
</style>
